package com.jic.market.mapper;

import com.jic.market.entity.DiscountConditionGiftDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 赠品明细表
 * @author : 
 * @email : 
 * @since : 2020-05-12 03:15:45
 * @version : v1.0.0
 */
@Mapper
public interface  DiscountConditionGiftDetailMapper {

    long insert(DiscountConditionGiftDetail discountConditionGiftDetail);

    long delete(Long id);

    long update(DiscountConditionGiftDetail discountConditionGiftDetail);

    DiscountConditionGiftDetail queryByPrimaryKey(Long id);

    List<DiscountConditionGiftDetail> query(DiscountConditionGiftDetail discountConditionGiftDetail);

    List<DiscountConditionGiftDetail> queryPage(@Param("startIndex") int startIndex, @Param("limit") int limit, @Param("discountConditionGiftDetail") DiscountConditionGiftDetail discountConditionGiftDetail);

    long queryPageCount(DiscountConditionGiftDetail discountConditionGiftDetail);

    @Select("select count(g.num) from discount_condition_gift_detail g LEFT JOIN discount_condition_promotion_detail c ON g.promotion_serial_no = c.promotion_serial_no" +
            " where c.promotion_code = #{promotionCode} and g.third_sku_id = #{thirdSkuId} and g.delete_flag = 0 and c.delete_flag = 0")
    Long countGoodsNum(@Param("promotionCode")String promotionCode, @Param("thirdSkuId")String thirdSkuId);
    @Select("select count(g.num) from discount_condition_gift_detail g LEFT JOIN discount_condition_promotion_detail c ON g.promotion_serial_no = c.promotion_serial_no" +
            " where c.promotion_code = #{promotionCode} and g.coupon_promotion_id = #{couponPromotionId} and g.delete_flag = 0 and c.delete_flag = 0")
    Long countCouponNum(@Param("promotionCode")String promotionCode, @Param("couponPromotionId")String couponPromotionId);

    @Select("select count(g.num) from discount_condition_gift_detail g LEFT JOIN discount_condition_promotion_detail c ON g.promotion_serial_no = c.promotion_serial_no" +
            " LEFT JOIN discount_order_detail o ON g.order_no = o.order_no " +
            "where c.promotion_code = #{promotionCode} and g.third_sku_id = #{thirdSkuId} and o.third_user_id = #{thirdUserId} and g.delete_flag = 0 and o.delete_flag = 0 and c.delete_flag = 0")
    Long countGoodsNumByUser(@Param("promotionCode")String promotionCode, @Param("thirdSkuId")String thirdSkuId, @Param("thirdUserId")String thirdUserId);
    @Select("select count(g.num) from discount_condition_gift_detail g LEFT JOIN discount_condition_promotion_detail c ON g.promotion_serial_no = c.promotion_serial_no" +
            " LEFT JOIN discount_order_detail o ON g.order_no = o.order_no " +
            "where c.promotion_code = #{promotionCode} and g.coupon_promotion_id = #{couponPromotionId} and o.third_user_id = #{thirdUserId} and g.delete_flag = 0 and o.delete_flag = 0 and c.delete_flag = 0")
    Long countCouponNumByUser(@Param("promotionCode")String promotionCode, @Param("couponPromotionId")String couponPromotionId, @Param("thirdUserId")String thirdUserId);
}